package cell 
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import ru.gosoplata.model.TmpModel;
	/**
	 * ...
	 * @author a.orleansky
	 */
	public class KeyBoardBtn extends Sprite 
	{
		public static var KEY_PAD_BTN_CLICK:String = "KEY_PAD_BTN_CLICK";
		public static var KEY_PAD_BTN_MANAGE_CLICK:String = "KEY_PAD_BTN_MANAGE_CLICK";
		public static var KEY_PAD_BTN_LOADED:String = "KEY_PAD_BTN_LOADED";
		private var index:String;
		private var image_up:Bitmap;
		private var image_down:Bitmap;
		private var icon_image:Bitmap;
		
		public function KeyBoardBtn() 
		{
			
		}
		
		private var eng_small:String="";
		private var eng_big:String="";
		private var rus_small:String="";
		private var rus_big:String="";
		private var symb1:String="";
		private var symb2:String="";
		private var manage:String = "";
		private var btn_pic_name:String;
		private var btn_icon:String = "";
		
		public function init(symbols:Array):void { 
			
			btn_pic_name = "but_key";
			if ((symbols[0] as String).length > 2) {
				manage = symbols[0];
				index = manage;
				switch( manage) {
					case "tab": btn_pic_name = "but_tab";btn_icon = btn_pic_name.substr(3); break;
					case "capslock": btn_pic_name = "but_capslosk";btn_icon = btn_pic_name.substr(3); break;
					case "shift": btn_pic_name = "but_shift";btn_icon = btn_pic_name.substr(3); break;
					case "lang": btn_pic_name = "but_lang";btn_icon = btn_pic_name.substr(3); break;
					case "spacebar": btn_pic_name = "but_space"; break;
					case "clear": btn_pic_name = "but_clean_input";btn_icon = btn_pic_name.substr(3); break;
					case "backspace": btn_pic_name = "but_backspace"; btn_icon = btn_pic_name.substr(3);break;
					case "enter": btn_pic_name = "but_enter";btn_icon = btn_pic_name.substr(3); break;
					case "magic": btn_pic_name = "but_magic"; break;
				}
			}else if((symbols[0] as String).length==2) {
				eng_small = (symbols[0] as String).charAt(0);
				eng_big = (symbols[0] as String).charAt(1);
				rus_small = (symbols[1] as String).charAt(0);
				rus_big = (symbols[1] as String).charAt(1);
				btn_pic_name = "but_key";
				index = eng_big;
			}else if ((symbols[0] as String).length == 1) {
				symb1 = (symbols[0] as String);
				if ((symbols[1] as String).length == 1) {
					symb2 = (symbols[1] as String);
				}else {
					rus_small = (symbols[1] as String).charAt(0);
					rus_big = (symbols[1] as String).charAt(1);
				}
				if(symb1=="|") btn_pic_name = "but_key_slash";
				index = symb1;
			}else {
				rus_small = (symbols[1] as String).charAt(0);
				rus_big = (symbols[1] as String).charAt(1);
				btn_pic_name = "but_key";
				index = rus_big;
			}
			image_up = new Bitmap();
			image_down = new Bitmap();
			drawFon();
			drawName();
			addEventListener(MouseEvent.CLICK, onPress);
		}
		
		private var color:String;
		
		public function drawFon():void {

			color = "blue";
			var url:String = TmpModel.IMG_FOLDER+"/keyboard_operatori/" + btn_pic_name + ".png";
			var req:URLRequest = new URLRequest(url);
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplite);
			
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent):void { } );
			loader.load(req);
			var url3:String = TmpModel.IMG_FOLDER+"/keyboard_operatori/"+btn_pic_name+"_down.png";
			var req3:URLRequest = new URLRequest(url3);
			var loader3:Loader = new Loader();
			loader3.contentLoaderInfo.addEventListener(Event.COMPLETE, loadCompliteDown);
			
			loader3.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent):void { } );
			loader3.load(req3);

		}
		
		//private function onLoadingError(e:IOErrorEvent):void 
		//{
		//}
		
		private function loadCompliteIcon(e:Event):void 
		{
			icon_image = Bitmap(Loader(e.target.loader).content);
			addChild(icon_image);
			
		}
		private var swLoadUP:Boolean = false;
		private var swLoadDown:Boolean = false;
		private function loadComplite(e:Event):void 
		{
			image_up = Bitmap(Loader(e.target.loader).content);
			addChild(image_up);
			swLoadUP = true;
			if (swLoadDown) finishInit();
			//var url3:String = TmpModel.IMG_FOLDER+"/keyboard_operatori/"+btn_pic_name+"_down.png";
			//var req3:URLRequest = new URLRequest(url3);
			//var loader3:Loader = new Loader();
			//loader3.contentLoaderInfo.addEventListener(Event.COMPLETE, loadCompliteDown);
			//loader3.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onLoadingError);
			//loader3.load(req3);
		}
		
		private	var txt:TextField = new TextField();
		private var txt2:TextField = new TextField();
		
		private function loadCompliteDown(e:Event):void {
			image_down = Bitmap(Loader(e.target.loader).content);
			image_down.visible = false;
			addChild(image_down);
			swLoadDown = true;
			if (swLoadUP) finishInit();
			//dispatchEvent(new Event( KEY_PAD_BTN_LOADED, true));
		}
		
		private function finishInit():void {
			dispatchEvent(new Event( KEY_PAD_BTN_LOADED, true));
			if (btn_icon != "") {
				var url2:String = TmpModel.IMG_FOLDER+"/keyboard_operatori/icon_keyb" + btn_icon + ".png";
				var req2:URLRequest = new URLRequest(url2);
				var loader2:Loader = new Loader();
				loader2.contentLoaderInfo.addEventListener(Event.COMPLETE, loadCompliteIcon);
			loader2.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent):void { } );
				loader2.load(req2);
			}else{
				txt.antiAliasType = AntiAliasType.ADVANCED;
				txt.text = (symb1!="")?symb1:eng_big;
				txt.thickness = 1;
				txt.width = 40;
				txt.height = 40;
				txt.x = 5;
				txt.y = 0;
				txt.embedFonts = true;
				txt.selectable = false;
				txt.setTextFormat(TmpModel.getTextFormat(34, 0xFFFFFF,true));
				addChild(txt);
				txt2.antiAliasType = AntiAliasType.ADVANCED;
				txt2.text = (symb2!="")?symb2:rus_big;
				txt.thickness = 100;
				txt2.width = 40;
				txt2.height = 40;
				txt2.x = 25;
				txt2.y = 25;
				txt2.embedFonts = true;
				txt2.selectable = false;
				txt2.setTextFormat(TmpModel.getTextFormat(30, 0xFFFFFF));
				addChild(txt2);
			}
			reDrawTextOnBtns("rus", true, false);
		}
		
		private var lang:String = "rus";
		private var isUppercase:Boolean = true;
		private var caps:Boolean = false;
		private var fixCaps:Boolean = false;
		private static var fixShift:Boolean = false;
		
		
		public function reDrawTextOnBtns(_lang:String,shift:Boolean, _caps:Boolean):void {
			lang = _lang;
			isUppercase = shift;
			caps = _caps;
			var fmt:TextFormat = TmpModel.getTextFormat(34, 0xFFFFFF,true);
			var fmt2:TextFormat = TmpModel.getTextFormat(30, 0xFFFFFF);
			if(eng_big.length>0){
			var _symbol:String;
				_symbol = (isUppercase||caps)?eng_big:eng_small;
				txt.text = _symbol;
				txt.setTextFormat(lang=="eng"?fmt:fmt2);
				_symbol = (isUppercase||caps)?rus_big:rus_small;
				txt2.text = _symbol;
				txt2.setTextFormat(lang=="eng"?fmt2:fmt);
			}
			
			if (symb1.length == 1) {
				if (rus_big.length > 0) {
					_symbol = (isUppercase||caps)?rus_big:rus_small;
					txt2.text = _symbol;
					txt2.setTextFormat(lang=="eng"?fmt2:fmt);
					txt.setTextFormat(lang=="eng"?fmt:fmt2);
				}else {
					txt.setTextFormat(isUppercase?fmt2:fmt);
					txt2.setTextFormat(isUppercase?fmt:fmt2);
				}
			}
			
			if (symb1.length == 0 && rus_big.length>0) {
					_symbol = (isUppercase||caps)?rus_big:rus_small;
					txt2.text = _symbol;
					txt2.setTextFormat(lang=="eng"?fmt2:fmt);
			}
			
			if (manage == "shift" && !fixShift) {
					image_up.visible = true;
					image_down.visible = false;
					try{
					if (btn_icon != "") icon_image.alpha = 1;
					else txt.alpha = 1;
					}catch (e:Error) {
						
					}
			}
		}
		
		public var timer:Number;
		private function showDownBtn(e:Event):void 
		{
			timer++;
			if (timer == 5) {
				if((manage!="shift"&&manage!="capslock")||(manage == "capslock" && !fixCaps)){
					image_up.visible = true;
					image_down.visible = false;
					if (btn_icon != "") icon_image.alpha = 1;
					else txt.alpha = 1;
				}
				removeEventListener(Event.ENTER_FRAME, showDownBtn);
				
				if (manage == "") {
					if (eng_big.length > 0) {
						if (lang == "eng") index = (isUppercase || caps)?eng_big:eng_small;
						else index = (isUppercase || caps)?rus_big:rus_small;
					}else if (rus_big.length > 0) {
						if (lang == "eng" && eng_big.length == 0) index = symb1;
						else index = (isUppercase || caps)?rus_big:rus_small;
					}else {
						index = isUppercase?symb2:symb1;
					}
					dispatchEvent(new Event(KEY_PAD_BTN_CLICK, true));
				}else {
					dispatchEvent(new Event(KEY_PAD_BTN_CLICK, true));
				}
				
			}
		}		
		public function drawName():void {
		}
		
		public function onPress(e:Event):void {
			timer = 0;
			image_up.visible = false;
			image_down.visible = true;
			switch(manage) {
				case "shift":
					fixShift = !fixShift;
				break;
				case "capslock":
					fixCaps = !fixCaps;
					fixShift = false;
				break;
				default :
				fixShift = false;
				break;
			}
			if (btn_icon != "") icon_image.alpha = 0.6;
			else txt.alpha = 0.6;
			addEventListener(Event.ENTER_FRAME, showDownBtn);
		}
		
		public function get indx():String {
			return index;
		}
		
		public function set indx(ind:String):void {
			index = ind;
		}
	}

}